﻿using DeviceAlarmService.Models;
using DeviceAlarmService.Models.SiemensPlc;
using S7.Net;

namespace DeviceAlarmService.DeviceManagement.DeviceConnection.SiemensPlc;

/// <summary>
/// 配置生成
/// </summary>
public class ConfigurationGeneration
{
    private Models.SiemensPlc.DeviceInformation _deviceInformation = new();

    public Models.SiemensPlc.DeviceInformation deviceInformation
    {
        get
        {
            return _deviceInformation;
        }
    }

    public void GenerationDeviceInformation(string number, string name, string protocol)
    {
        _deviceInformation.number = number;
        _deviceInformation.name = name;
        _deviceInformation.protocol = (Protocol)Enum.Parse(typeof(Protocol), protocol, true);
    }

    public void GenerationDeviceAddress(string type, string ip, string port, string rack, string slot, string isMultiple)
    {
        _deviceInformation.deviceAddress = new DeviceAddress()
        {
            cpuType = (CpuType)Enum.Parse(typeof(CpuType), type.ToUpper(), true),
            ip = ip,
            port = short.Parse(port),
            rack = short.Parse(rack),
            slot = short.Parse(slot),
            isMultiple = bool.Parse(isMultiple)

        };
    }

    public void GenerationContacts(string phoneNumber, string level, string enableDialing, string enableSms)
    {
        if (_deviceInformation.contacts == null)
        {
            _deviceInformation.contacts = new List<Contacts>();
        }
        _deviceInformation.contacts.Add(new Contacts()
        {
            phoneNumber = phoneNumber,
            level = (Level)Enum.Parse(typeof(Level), level, true),
            enableSms = bool.Parse(enableSms),
            enableDialing = bool.Parse(enableDialing)
        });
    }


    public void GenerationDataAddress(string alarmAddress, string isEnable, string alarmInformation)
    {
        if (alarmAddress.Length < 2)
        {
            return;
        }
        if (_deviceInformation.dataAddress == null)
        {
            _deviceInformation.dataAddress = new List<DataAddress>();
        }
        _deviceInformation.dataAddress.Add(new DataAddress()
        {
            alarmAddress = alarmAddress.ToUpper(),
            isEnable = bool.Parse(isEnable),
            alarmInformation = alarmInformation
        });
    }
}